using Microsoft.AspNetCore.Mvc;
using UniversalAdminSystem.Application.LogManagement.DTOs;
using UniversalAdminSystem.Application.LogManagement.Interfaces;
using UniversalAdminSystem.Api.Attributes;
using UniversalAdminSystem.Application.Common.Results;

namespace UniversalAdminSystem.Api.Controllers;

[ApiController]
[Route("api/logs")]
public class LogController : ControllerBase
{
    private readonly ILogManagementAppService _logService;
    public LogController(ILogManagementAppService logService) => _logService = logService;

    [HttpGet]
    //[RequirePermission("log:read")]
    public async Task<IActionResult> GetAllLogsAsync()
    {
        var result = await _logService.GetAllLogsAsync();
        return result.IsSuccess ? Ok(result) : BadRequest(result);
    }

    [HttpGet("{id}")]
    //[RequirePermission("log:read")]
    public async Task<IActionResult> GetLogByIdAsync(Guid id)
    {
        var result = await _logService.GetLogByIdAsync(id);
        return result.IsSuccess ? Ok(result) : BadRequest(result);
    }

    [HttpPost]
    //[RequirePermission("log:create")]
    public async Task<IActionResult> CreateLogAsync([FromBody] LogCreateDto dto)
    {
        var result = await _logService.CreateLogAsync(dto);
        return result.IsSuccess ? Ok(result) : BadRequest(result);
    }

    [HttpGet("level/{level}")]
    //[RequirePermission("log:read")]
    public async Task<IActionResult> GetLogsByLevelAsync(string level)
    {
        var result = await _logService.GetLogsByLevelAsync(level);
        return result.IsSuccess ? Ok(result) : BadRequest(result);
    }

    [HttpGet("user/{userId}")]
    //[RequirePermission("log:read")]
    public async Task<IActionResult> GetLogsByUserAsync(Guid userId)
    {
        var result = await _logService.GetLogsByUserAsync(userId);
        return result.IsSuccess ? Ok(result) : BadRequest(result);
    }

    [HttpGet("date")]
    //[RequirePermission("log:read")]
    public async Task<IActionResult> GetLogsByDateRangeAsync([FromQuery] DateTime start, [FromQuery] DateTime end)
    {
        var result = await _logService.GetLogsByDateRangeAsync(start, end);
        return result.IsSuccess ? Ok(result) : BadRequest(result);
    }

    [HttpGet("source/{source}")]
    //[RequirePermission("log:read")]
    public async Task<IActionResult> GetLogsBySourceAsync(string source)
    {
        var result = await _logService.GetLogsBySourceAsync(source);
        return result.IsSuccess ? Ok(result) : BadRequest(result);
    }
} 